package com.qqbot.impl.utils;

import com.qqbot.impl.thread.StreamInformatonRunnable;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Windows系统命令执行工具类
 *
 * @author changwenpeng
 */
@Slf4j
public class WindowsCmdUtils {

    private static String charsetName = "gb2312";

    /**
     * 执行cmd命令 执行完后关闭cmd窗口
     *
     * @param command
     * @return
     * @throws IOException
     */
    public static String executeCmd(String command) throws IOException {
        log.info("Execute command : " + command);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("cmd /c " + command);
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), charsetName));
        String line = null;
        StringBuilder build = new StringBuilder();
        while ((line = br.readLine()) != null) {
            build.append(line);
        }
        return build.toString();
    }

    /**
     * 执行cmd命令 将命令生成bat文件，然后cmd执行bat文件
     *
     * @param command     要执行的命令
     * @param filePath    bat文件
     * @param batCommands bat文件内容
     * @return
     * @throws IOException
     */
    public static void executeCmds(String command, String filePath, String batCommands) {
        FileUtils.fileLinesWrite(filePath, batCommands, false);
        log.info("Execute command : " + command);
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("cmd /c start " + command);
            //BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), charsetName));
            //开启线程读取错误输出，避免阻塞
            //new Thread(new StreamInformatonRunnable(br)).start();
        } catch (Exception e) {
            log.error("Execute command:{}, error:{}", command, e);
        }
    }

    /**
     * 杀死进程
     *
     * @param processName 进程名称
     * @throws IOException
     */
    public static void killProc(String processName) throws IOException {
        log.info("关闭应用程序：" + processName);
        if (StringUtils.isNotBlank(processName)) {
            executeCmd("taskkill /F /IM " + processName);
        }
    }

    /**
     * 判断进程是否开启
     *
     * @param processName 进程名称
     * @return
     */
    public static boolean findProcess(String processName) {
        BufferedReader bufferedReader = null;
        try {
            Process proc = Runtime.getRuntime().exec("tasklist -fi " + '"' + "imagename eq " + processName + '"');
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(processName)) {
                    return true;
                }
            }
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }
    }

}